package com.hds.common.utils;

import java.io.IOException;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpUtils {
	
	/**
	 * get请求
	 * @param url
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 */
	@SuppressWarnings("deprecation")
	public static JSONObject doGetStr(String url) throws ParseException, IOException{
		DefaultHttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		JSONObject jsonObject = null;
		HttpResponse httpResponse = client.execute(httpGet);
		HttpEntity entity = httpResponse.getEntity();
		if(entity != null){
			String result = EntityUtils.toString(entity,"UTF-8");
			jsonObject = JSONObject.parseObject(result);
		}
		httpGet.releaseConnection();
		return jsonObject;
	}
	
	/**
	 * POST请求
	 * @param url
	 * @param outStr
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 */
	@SuppressWarnings("deprecation")
	public static JSONObject doPostStr(String url,String outStr) throws ParseException, IOException{
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost httpost = new HttpPost(url);
		JSONObject jsonObject = null;
		httpost.setEntity(new StringEntity(outStr,"UTF-8"));
		HttpResponse response = client.execute(httpost);
		String result = EntityUtils.toString(response.getEntity(),"UTF-8");
		jsonObject = JSONObject.parseObject(result);
		httpost.releaseConnection();
		return jsonObject;
	}
	
	
	
	/**
	  * 访问url 返回xml javabean
	  * @return
	  */
	 public static String getXml(String url) {

	  HttpClient httpClient = new HttpClient();
	  // 创建GET方法的实例
	  GetMethod getMethod = new GetMethod(url);
	  // 使用系统提供的默认的恢复策略
	  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
	    new DefaultHttpMethodRetryHandler());
	  String xml = null;
	  try {
	   // 执行getMethod
	   int statusCode = httpClient.executeMethod(getMethod);
	   if (statusCode != HttpStatus.SC_OK) {
	    System.err.println("Method failed: "
	      + getMethod.getStatusLine());
	   }
	   // 读取内容
	   byte[] responseBody = getMethod.getResponseBody();
	   // 处理内容
	//   System.out.println(new String(responseBody, "GBK"));
	   xml = new String(responseBody, "UTF-8");
	  } catch (Exception e) {
	   // 发生致命的异常，可能是协议不对或者返回的内容有问题
	   System.out.println("Please check your provided http address!");
	   e.printStackTrace();
	   System.out.println(e.getMessage());
	   return xml;
	  } finally {
	   // 释放连接
	   getMethod.releaseConnection();
	  }
	  return xml;
	 }

}
